#include "TextModPlugin.h"

#include <QtGui/QColorDialog>
#include <QtGui/QToolBar>

#include "DateFormatDlg.h"

TextModPlugin::TextModPlugin() {
	//	context menu
	ctxMenu_ = new QMenu(tr("Selected text..."));
	ctxMenu_->addAction(tr("TO UPPER CASE"), this, SLOT(toUpper()));
	ctxMenu_->addAction(tr("to lower case"), this, SLOT(toLower()));
	QMenu* htmlMenu = ctxMenu_->addMenu("HTML");
	htmlMenu->addAction(tr("<a href ... >"), this, SLOT(htmlHref()));
	htmlMenu->addAction(tr("<b>...</b>"), this, SLOT(htmlBold()));
	htmlMenu->addAction(tr("<i>...</i>"), this, SLOT(htmlItalic()));
	htmlMenu->addAction(tr("<u>...</u>"), this, SLOT(htmlUnderline()));
	htmlMenu->addAction(tr("<div>...</div>"), this, SLOT(htmlDiv()));
	htmlMenu->addAction(tr("<ul>...</ul>"), this, SLOT(htmlUList()));
	htmlMenu->addAction(tr("<ol>...</ol>"), this, SLOT(htmlOList()));

	//	main menu
	menu_ = new QMenu(tr("Insert"));
	menu_->addAction(tr("Current Date and Time"), this, SLOT(date()));
	htmlMenu = menu_->addMenu("HTML");
	htmlMenu->addAction(tr("File template"), this, SLOT(htmlTemplate()));
	htmlMenu->addAction(tr("<table> ... "), this, SLOT(htmlTable()));
	htmlMenu->addAction(tr("<a href ... >"), this, SLOT(htmlHref()));
	htmlMenu->addAction(tr("<img ... >"), this, SLOT(htmlImg()));
	htmlMenu->addAction(tr("<b>...</b>"), this, SLOT(htmlBold()));
	htmlMenu->addAction(tr("<i>...</i>"), this, SLOT(htmlItalic()));
	htmlMenu->addAction(tr("<u>...</u>"), this, SLOT(htmlUnderline()));
	htmlMenu->addAction(tr("<div>...</div>"), this, SLOT(htmlDiv()));
	htmlMenu->addAction(tr("<ul>...</ul>"), this, SLOT(htmlUList()));
	htmlMenu->addAction(tr("<ol>...</ol>"), this, SLOT(htmlOList()));
	
	toolBar_ = new QToolBar("TextMod");
	toolBar_->addAction(QIcon(":color_icon"), "Insert a color", this, SLOT(callColorDlg()));
}

TextModPlugin::~TextModPlugin() {
	delete ctxMenu_;
	delete menu_;
	delete toolBar_;
}

QString TextModPlugin::name() const {
	return "TextMod";
}

QString TextModPlugin::description() const {
	return "This plugin can insert different pre-defined text templates and convert existing text";
}

QString TextModPlugin::targetEngine() const {
	return "sci";
}

Juff::ActionList TextModPlugin::contextMenuActions() const {
	Juff::ActionList list;
	list << ctxMenu_->menuAction();
	return list;
}

QMenu* TextModPlugin::menu() const {
	return menu_;
}

QToolBar* TextModPlugin::toolBar() const {
	return toolBar_;
}
void TextModPlugin::toUpper() {
	QString selText;
	manager()->getSelectedText(selText);
	if ( !selText.isEmpty() )
		manager()->replaceSelectedText(selText.toUpper());
}

void TextModPlugin::toLower() {
	QString selText;
	manager()->getSelectedText(selText);
	if ( !selText.isEmpty() )
		manager()->replaceSelectedText(selText.toLower());
}

void TextModPlugin::date() {
	DateFormatDlg dlg;
	
	if ( dlg.exec() == QDialog::Accepted ) {
		QString fmt = dlg.format();
		QDateTime dt = QDateTime::currentDateTime();
		QString dateStr = dt.toString(fmt);
		int row, col;
		manager()->getCursorPos(row, col);
		manager()->insertText(dateStr);
		manager()->setCursorPos(row, col + dateStr.length());
	}
}

void TextModPlugin::htmlTemplate() {
	QString text = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n"
			"<html>\n"
			"<head>\n"
			"<title></title>\n"
			"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" >\n"
			"<link type=\"text/css\" rel=\"stylesheet\" href=\"style.css\">\n"
			"</head>\n"
			"<body>\n\n"
			"</body>\n"
			"</html>\n";
	int row, col;
	manager()->getCursorPos(row, col);
	manager()->insertText(text);
	manager()->setCursorPos(row + 3, 7);
}

void TextModPlugin::htmlTable() {
	QString text = "<table>\n"
			"  <tr>\n"
			"    <td></td> <td></td>\n"
			"  </tr>\n"
			"  <tr>\n"
			"    <td></td> <td></td>\n"
			"  </tr>\n"
			"</table>\n";
	int row, col;
	manager()->getCursorPos(row, col);
	manager()->insertText(text);
	manager()->setCursorPos(row + 2, 8);
}

void TextModPlugin::wrapSelectedText(const QString& fmt, int indent) {
	QString selText;
	manager()->getSelectedText(selText);
	if ( !selText.isEmpty() ) {
		int row1, col1, row2, col2;
		manager()->getSelection(row1, col1, row2, col2);
		manager()->replaceSelectedText(fmt.arg(selText));
		manager()->setCursorPos(row1, col1 + indent);
	}
	else {
		int row, col;
		manager()->getCursorPos(row, col);
		manager()->insertText(fmt.arg(""));
		manager()->setCursorPos(row, col + indent);
	}
}

void TextModPlugin::htmlHref() {
	wrapSelectedText("<a href=\"\">%1</a>", 9);
}

void TextModPlugin::htmlImg() {
	QString text = "<img src=\"\" alt=\"\" />";
	int row, col;
	manager()->getCursorPos(row, col);
	manager()->insertText(text);
	manager()->setCursorPos(row, col + 10);
}

void TextModPlugin::htmlBold() {
	wrapSelectedText("<b>%1</b>", 3);
}

void TextModPlugin::htmlItalic() {
	wrapSelectedText("<i>%1</i>", 3);
}

void TextModPlugin::htmlUnderline() {
	wrapSelectedText("<u>%1</u>", 3);
}

void TextModPlugin::htmlDiv() {
	wrapSelectedText("<div>%1</div>", 5);
}

void TextModPlugin::createList(bool numerated) {
	QString tag = numerated ? "ol" : "ul";
	QString selText;
	manager()->getSelectedText(selText);

	if ( !selText.isEmpty() ) {
		//	set selection and split it into lines
		int row1, col1, row2, col2;
		manager()->getSelection(row1, col1, row2, col2);
		QStringList lines = selText.split(QRegExp("(\n|\r|\r\n)"));
		if ( col2 == 0 )	//	last string is empty
			lines.pop_back();
		
		//	create a list
		QString text = QString("<%1>\n").arg(tag);
		if ( col1 > 0 )		//	go to the next line
			text.prepend("\n");
		foreach (QString line, lines)
			text = text + "  <li>" + line + "\n";
		text += QString("</%1>").arg(tag);

		manager()->replaceSelectedText(text);
	}
	else {
		int row, col;
		manager()->getCursorPos(row, col);
		manager()->insertText(QString("<%1>\n  <li>\n  <li>\n  <li>\n</%2>").arg(tag).arg(tag));
		manager()->setCursorPos(row + 1, 6);
	}
}

void TextModPlugin::htmlUList() {
	createList(false);
}

void TextModPlugin::htmlOList() {
	createList(true);
}

void TextModPlugin::callColorDlg() {
	QColor color = QColorDialog::getColor();
	if ( color.isValid() ) {
		manager()->insertText(color.name());
	}
}


Q_EXPORT_PLUGIN2(textmod, TextModPlugin)
